#include "ftdNet.h"
#include "ftdLog.h"

using namespace ftd;

//---------------------------ftdNet--------------------------------------
FTDSTRING ftd_Net::getLocalIP()
{
	return "";
}
//---------------------------ftdNet--------------------------------------

//----------------------------ftd_Server---------------------------------
ftd_Server::ftd_Server()
{

}

ftd_Server::~ftd_Server()
{

}

FTDBOOL ftd_Server::init(FTDSTRING initAddr/*="127.0.0.1"*/,FTD_PROTOCOL_TYPE type/*=HTTP_POST*/)
{
	return FTDTRUE;
}

FTDBOOL ftd_Server::formatHead()
{
	//////////////////////////////////////////////////////////////////////////
	pHead = curl_slist_append(pHead,"HTTP-Version: HTTP/1.1");
	curl_slist_append(pHead,"Server host: 127.0.0.1:8080");
	curl_slist_append(pHead,"Content-Type: text/xml");
	curl_slist_append(pHead,"Connection: KeepAlive");

	if (NULL == pHead)
	{
		return FTDFALSE;
	}
	return FTDTRUE;
}

FTDBOOL ftd_Server::start()
{
	init();

	while ( receive() )
	{
		FTDBOOL ret = send();
		if ( !ret )
		{
			ftdLog::getInstance()->printLog("send information failed.");
		}
	}
	return FTDTRUE;
}

FTDBOOL ftd_Server::stop()
{
	return FTDTRUE;
}

FTDBOOL ftd_Server::send()
{
	CURLcode ret;
	CURL* tmpHandle = curl_easy_init();
	if (NULL == tmpHandle)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_init() call failed.");
		return FTDFALSE;
#endif
	}

	curl_easy_setopt(tmpHandle,CURLOPT_URL,"127.0.0.1");
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECT_ONLY,1);
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECTTIMEOUT,1);
	ret = curl_easy_perform(tmpHandle);
	if (CURLE_OK != ret)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_perform() call failed.");
		return FTDFALSE;
#endif
	}

	ret = curl_easy_getinfo(tmpHandle,CURLINFO_LASTSOCKET);
	if (CURLE_OK != ret)
	{
		ftdLog::getInstance()->printLog("curl_easy_getinfo() call failed.");
		return FTDFALSE;
	}

	if (formatHead())
	{
		curl_easy_setopt(tmpHandle, CURLOPT_HTTPHEADER, pHead);
	}
	else
	{
		ftdLog::getInstance()->printLog("curl_easy_getinfo() call failed.");
		return FTDFALSE;
	}

	FTDPTR buffer = new FTDCHAR[BODY_BUFFER_SIZE];
	SET_ZERO(buffer,BODY_BUFFER_SIZE);
	size_t len = GET_SIZE(buffer);
	SET_COPY(buffer,"test");
	curl_easy_send(tmpHandle,buffer,GET_SIZE(buffer),&len);
	return FTDTRUE;
}

FTDBOOL ftd_Server::parseHead()
{
	return FTDTRUE;
}

FTDBOOL ftd_Server::receive()
{
	return FTDTRUE;
}
//----------------------------ftd_Server---------------------------------

//---------------------------ftd_Client----------------------------------
ftd_Client::ftd_Client()
	: mBufferLength(0)
{

}

ftd_Client::~ftd_Client()
{

}

FTDBOOL ftd_Client::init(FTDSTRING initAddr/*="127.0.0.1"*/,FTD_PROTOCOL_TYPE type/*=HTTP_POST*/)
{
	return FTDTRUE;
}

FTDBOOL ftd_Client::start()
{
	return FTDTRUE;
}

FTDBOOL ftd_Client::stop()
{
	return FTDTRUE;
}

FTDBOOL ftd_Client::send()
{
	CURLcode ret;
	CURL* tmpHandle = curl_easy_init();
	if (NULL == tmpHandle)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_init() call failed.");
		return FTDFALSE;
#endif
	}

	curl_easy_setopt(tmpHandle,CURLOPT_URL,"192.168.1.103:8888");
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECT_ONLY,1);
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECTTIMEOUT,1);
	ret = curl_easy_perform(tmpHandle);

	if (CURLE_OK != ret)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_perform() call failed.");
		return FTDFALSE;
#endif
	}


	FTDPTR buffer = new FTDCHAR[BODY_BUFFER_SIZE];
	SET_ZERO(buffer,BODY_BUFFER_SIZE);
	size_t len = GET_SIZE(buffer);
	SET_COPY(buffer,"test");
	mBufferLength = strlen(buffer);


	if (formatHead())
	{
		curl_easy_setopt(tmpHandle, CURLOPT_HTTPHEADER, pHeadRequest);
	}
	else
	{
		ftdLog::getInstance()->printLog("curl_easy_getinfo() call failed.");
		return FTDFALSE;
	}
	curl_easy_send(tmpHandle,buffer,GET_SIZE(buffer),&len);
	return FTDTRUE;
}

FTDBOOL ftd_Client::receive()
{
	CURLcode ret;
	CURL* tmpHandle = curl_easy_init();
	if (NULL == tmpHandle)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_init() call failed.");
		return FTDFALSE;
#endif
	}

	curl_easy_setopt(tmpHandle,CURLOPT_URL,"192.168.1.103:8888");
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECT_ONLY,1);
	curl_easy_setopt(tmpHandle,CURLOPT_CONNECTTIMEOUT,1);
	ret = curl_easy_perform(tmpHandle);
	if (CURLE_OK != ret)
	{
#ifdef WIN32
		ftdLog::getInstance()->printLog("curl_easy_perform() call failed.");
		return FTDFALSE;
#endif
	}

// 	ret = curl_easy_getinfo(tmpHandle,CURLINFO_LASTSOCKET);
// 	if (CURLE_OK != ret)
// 	{
// 		ftdLog::getInstance()->printLog("curl_easy_getinfo() call failed.");
// 		return FTDFALSE;
// 	}

// 	if (formatHead())
// 	{
// 		curl_easy_setopt(tmpHandle, CURLOPT_HTTPHEADER, pHeadRequest);
// 	}
// 	else
// 	{
// 		ftdLog::getInstance()->printLog("curl_easy_getinfo() call failed.");
// 		return FTDFALSE;
// 	}

	FTDPTR buffer = new FTDCHAR[BODY_BUFFER_SIZE];
	SET_ZERO(buffer,BODY_BUFFER_SIZE);
	size_t len = BODY_BUFFER_SIZE;
	ret = curl_easy_recv(tmpHandle,buffer,GET_SIZE(buffer),&len);
	if (ret < 0)
	{
		ftdLog::getInstance()->printLog("curl_easy_recv() call failed.");
		return FTDFALSE;
	}
	else if (ret == CURLE_OK)
	{
		ftdLog::getInstance()->printLog(buffer);
	}

	return FTDTRUE;
}

FTDBOOL ftd_Client::formatHead()
{
	//////////////////////////////////////////////////////////////////////////
	pHeadRequest = curl_slist_append(NULL,"HTTP-Version: HTTP/1.1");
	curl_slist_append(pHeadRequest,"Host: 127.0.0.1:8080");
	curl_slist_append(pHeadRequest,"Content-Type: text/xml");
	curl_slist_append(pHeadRequest,"Connection: KeepAlive");
	curl_slist_append(pHeadRequest,"Accept-Charset: UTF-8");

// 	FTDINT tmpLength = strlen("Set-Cookie: ");
// 	FTDCHAR cookie[45];
// 	SET_ZERO(cookie,45);
// 	SET_COPY(cookie,"Set-Cookie: ");
// 	STR_CAT(cookie,szCookie);
// 	curl_slist_append(pHeadRequest,cookie);
	FTDCHAR len[10] = {0};
	INT_TO_STRING(mBufferLength,len,10);
	curl_slist_append(pHeadRequest,len);

	if (NULL == pHeadRequest)
	{
		return FTDFALSE;
	}
	return FTDTRUE;
}

FTDBOOL ftd_Client::parseHead()
{
	return FTDTRUE;
}
//---------------------------ftd_Client----------------------------------